package de.koller.oceanFramework.plain.fullParticle;

import de.koller.oceanFramework.tree.Sea3X;

/**
 * 
 * @author Fabian Koller
 */
public class PondGraphics extends Sea3X {
	/**
	 * 
	 */
	public final int[] pixels;
	
	private ParticleImage pimage;
	
	/**
	 * 
	 * @param pixels
	 */
	public PondGraphics( int[] pixels ) {
		this.pixels = pixels;
	}
	
	/**
	 * 
	 * @param color
	 * @param x
	 * @param y
	 */
	public void setPixel( int color, float x, float y ) {
		int i = offsetOf( x, y );
		if( i < 0 || i >= pixels.length )
			return;
		
		pixels[i] = color;
	}

	/**
	 * 
	 */
	@Override
	protected void init() {
		super.init();
		
		pimage = (ParticleImage)this.searchMother( ParticleImage.class );
	}
	
	/** Returns the array index of the coordinates
	 * @param x
	 * @param y 
	 * @return  
	 */
	public int offsetOf( float x, float y ) {
//		float ax = (x - 0 );
//		float ay = (y - viewport.y);
//		ax = Math.round( ax );
//		ay = Math.round( ay );
		if( x < 0 || y < 0 || x >= pimage.getBitmapWidth() || y >= pimage.getBitmapHeight() )
			return -1;
		return (int)y * pimage.getBitmapWidth() + (int)x;
	}
	
	/**
	 * 
	 * @param x
	 * @param y
	 * @return
	 */
	public int colorAt( float x, float y ) {
		return pixels[ offsetOf( x, y ) ];
	}
	
	/**
	 * 
	 * @param offset
	 * @return
	 */
	public float xFromOffset( int offset ) {
		int ax = offset - (int)((float)offset / (float)pimage.getBitmapWidth())*pimage.getBitmapWidth();
		return ax;
	}
	
	/**
	 * 
	 * @param offset
	 * @return
	 */
	public float yFromOffset( int offset ) {
		int ay = offset / pimage.getBitmapWidth();
		return ay;
	}
	
	/**
	 * 
	 * @param color
	 */
	public void clear( int color ) {
		for( int i = 0; i < pixels.length; i++ )
			pixels[i] = color;
	}

	/**
	 * 
	 * @return
	 */
	public int[] getPixels() {
		return pixels;
	}

}
